package com.lzm.ydpt.module.mall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.byt.framlib.commonutils.ToastUitl;
import com.byt.framlib.commonutils.image.ImageLoaderUtils;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.MallCaty;

import java.util.ArrayList;
import java.util.List;

public class HorizontalGvAdapter extends BaseAdapter {
    private int CATY_SIZE = 10;
    private Context mContext;
    private List<MallCaty> mAppDatas = new ArrayList<>();

    public HorizontalGvAdapter(Context context, List<MallCaty> list, int page) {
        this.mContext = context;
        // 开始加载的位置
        int pageStart = page * CATY_SIZE;
        // 结束加载的位置
        int pageEnd = pageStart + CATY_SIZE;

        while ((pageStart < list.size()) && (pageStart < pageEnd)) {
            mAppDatas.add(list.get(pageStart));
            pageStart++;
        }
    }

    @Override
    public int getCount() {
        return mAppDatas.size();
    }

    @Override
    public MallCaty getItem(int position) {
        return mAppDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_mall_caty_home_gv, null);
            holder.img = convertView.findViewById(R.id.img_show_caty);
            holder.tvName = convertView.findViewById(R.id.tv_show_caty_name);
            holder.ll_caty_home_jump = convertView.findViewById(R.id.ll_caty_home_jump);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        MallCaty mallCaty = getItem(position);
        ImageLoaderUtils.displayCircle(holder.img, mallCaty.getImage_src());
        holder.tvName.setText(mallCaty.getCatyName());
        holder.ll_caty_home_jump.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ToastUitl.showShort("跳转");
            }
        });
        return convertView;
    }

    class Holder {
        ImageView img;
        TextView tvName;
        LinearLayout ll_caty_home_jump;
    }
}
